package com.github.chain.admin.server.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.chain.admin.common.domain.AdminLogger;

import com.github.chain.admin.common.request.AdminLoggerRequest;
import com.github.chain.admin.server.dao.AdminLoggerDao;
import com.github.chain.admin.server.service.AdminLoggerService;
import com.github.framework.core.Result;
import com.github.framework.core.page.DataPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author AIQ
 * @date 2023/5/25
 * @project er-trader
 */

@Service
@Slf4j
public class AdminLoggerServiceImpl implements AdminLoggerService {

    @Resource
    private AdminLoggerDao adminLoggerDao;

    @Override
    public Result<DataPage<AdminLogger>> findLogByPage(AdminLoggerRequest adminLoggerRequest) {

        Map<String, Object> map = BeanUtil.beanToMap(adminLoggerRequest);

        if (StringUtils.isBlank(adminLoggerRequest.getAsc()) || StringUtils.isBlank(adminLoggerRequest.getOrderByColumn())){
            adminLoggerRequest.setAsc("desc");
            adminLoggerRequest.setOrderByColumn("create_date");
        }
        int pageNo = adminLoggerRequest.getPageNo();
        int pageSize = adminLoggerRequest.getPageSize();

        DataPage<AdminLogger> dataPage = new DataPage<>(pageNo, pageSize);

        map.put("start", dataPage.getStartIndex());
        map.put("offset", dataPage.getPageSize());

        long totalCount = adminLoggerDao.findAdminLoggerCountByPage(map);
        List<AdminLogger> list = adminLoggerDao.findAdminLoggerListByPage(map);

        dataPage.setDataList(list);
        dataPage.setTotalCount(totalCount);

        return Result.ok(dataPage);
    }

    @Override
    public Result<Integer> insertSelective(AdminLogger adminLogger) {
        Integer status = adminLoggerDao.insertSelective(adminLogger);
        return Result.ok(status);
    }
}
